package net.huashitong.scheduling.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;

import net.huashitong.Indicators.filter.IndicatorBreakdownFilter;
import net.huashitong.Indicators.utils.DataUtils;
import net.huashitong.scheduling.entity.CorporateTaxation;
import net.huashitong.scheduling.entity.FinanceIncome;
import net.huashitong.scheduling.entity.IndustryRevenue;
import net.huashitong.scheduling.filter.IndicatorSchedulingFilter;
import net.huashitong.scheduling.filter.ResourcesFilter;
import net.huashitong.scheduling.service.CorporateTaxationService;
import net.huashitong.scheduling.service.FinanceIncomeService;
import net.huashitong.scheduling.service.IndustryRevenueService;
import net.huashitong.scheduling.utils.IsUserUtils;
@Controller
@RequestMapping("/app/revenue/")
public class RevenueController extends JsonBaseController {
	
	@Autowired
	private IndustryRevenueService industryRevenueService;
	@Autowired
    private CorporateTaxationService corporateTaxationService;
	@Autowired
	private FinanceIncomeService financeIncomeService;
	@Override
	protected String getView(String arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	public static final String[] months = {"12","11","10","09","08","07","06","05","04","03","02","01"};
	public static final String[] years={"2022", "2021", "2020","2019","2018","2017"};
	@ResponseBody
	@RequestMapping("/taxComplete")
	public Message queryTertiaryIndustrytotal(@RequestBody IndicatorBreakdownFilter filter){
	  Map<String, Object> map = new HashMap<String, Object>();
	  List<Map<String, Object>> lists = new ArrayList<Map<String,Object>>();
	  IsUserUtils isuts=new IsUserUtils();
	  List<CorporateTaxation> ctList=new ArrayList<CorporateTaxation>();
	  //税收分产完成统计
    List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
  	List<Map<String, Object>> incomelists = new ArrayList<Map<String,Object>>();
  	IndustryRevenue ir=new IndustryRevenue();
  	String time="";
  	int ss=0;
  	if(ObjectUtils.isBlank(filter.getDate())){
  	for(int j=0;j<RevenueController.years.length;j++){
  		if(ss==1){
			 break;
		 }
  	   for(int i=0; i<RevenueController.months.length;i++){
  		ir=industryRevenueService.queryByName("合计",months[i],years[j]);
  		ctList=corporateTaxationService.queryCtListByDate(months[i],years[j]);
  		 if(ObjectUtils.isNotBlank(ir)||ObjectUtils.isNotBlank(ctList)){
  			time=years[j]+"年"+months[i]+"月";
  			ss=1;
  			break;
        }
  		
  	  }
  	 }
  	}else{
  		System.out.println("时间="+filter.getDate());
  		  String month=filter.getDate().substring(4,6);
  		  String year=filter.getDate().substring(0,4);
  		 ir=industryRevenueService.queryByName("合计",month,year);
  		 ctList=corporateTaxationService.queryCtListByDate(month,year);
  	}
  	if(ObjectUtils.isNotBlank(ir)){
  	List<IndustryRevenue> irlist=industryRevenueService.queryBySysId(ir.getSysId());
  	for(IndustryRevenue irss:irlist){
		Map<String, Object> map2 = new HashMap<String, Object>();
		map2.put("name", irss.getName());
		map2.put("currentYear", irss.getYearIncome());
		incomelists.add(map2);
		Map<String, Object> map3 = new HashMap<String, Object>();
		map3.put("name",irss.getName());
		map3.put("currentYear", irss.getYearIncome());
    	map3.put("lastYear", irss.getLastyearIncome());
    	map3.put("increDecre", irss.getIncreDecre());
    	map3.put("totalRatio", irss.getTotalRatio());
    	map3.put("sysId", irss.getSysId());
    	map3.put("units", irss.getUnits());
    	list.add(map3);
	 }
  	map.put("type", "12");
  	map.put("title","该指标分行业完成情况");
	map.put("currentYear", ir.getYearIncome());
	map.put("lastYear", ir.getLastyearIncome());
	map.put("increDecre", ir.getIncreDecre());
	map.put("totalRatio", ir.getTotalRatio());
	map.put("units", ir.getUnits());
	map.put("industryList",incomelists);
	map.put("industryData",list);
	lists.add(map);
  	}
	//企业税收完成情况
	List<Map<String, Object>> businesslist1 = new ArrayList<Map<String,Object>>();
	List<Map<String, Object>> businesslist2 = new ArrayList<Map<String,Object>>();
	Map<String, Object> maps = new HashMap<String, Object>();
	Map<String, Object> maps3 = new HashMap<String, Object>();
	Map<String, Object> maps4= new HashMap<String, Object>();
   if(ObjectUtils.isNotBlank(ctList)){
	int i=1;
	for(CorporateTaxation ct:ctList){
		Map<String, Object> map2 = new HashMap<String, Object>();
		/*if(i<=20){*/
			map2.put("id", ct.getSysId());
			map2.put("name", ct.getName());
			map2.put("accumulativeStorage",ct.getAccumulativeStorage());
			map2.put("increDecreValue",ct.getIncreDecreValue());
			map2.put("increDecreaRatio",ct.getIncreDecreaRatio());
			map2.put("units",ct.getUnits());
			if(ct.getIncreDecreaRatio().indexOf("-")==-1){
				map2.put("yujz","1");
			}else{
				map2.put("yujz","0");
			}
			 businesslist1.add(map2);
		/*}else{
			map2.put("id", ct.getSysId());
			map2.put("name", ct.getName());
			map2.put("accumulativeStorage",ct.getAccumulativeStorage());
			map2.put("increDecreValue",ct.getIncreDecreValue());
			map2.put("increDecreaRatio",ct.getIncreDecreaRatio());
			map2.put("units", ct.getUnits());
			if(ct.getIncreDecreaRatio().indexOf("-")==-1){
				map2.put("yujz","1");
			}else{
				map2.put("yujz","0");
			}
			 businesslist2.add(map2);
		}
		i++;*/
	 }
	/*List alllist  =new ArrayList();*/

	maps3.put("type","13");
	maps4.put("type","13");
	maps3.put("page", "1");
	maps4.put("page", "2");
	maps3.put("units",ctList.get(0).getUnits());
	maps4.put("units",ctList.get(0).getUnits());
	maps3.put("title", "税源重点监测企业税收完成情况(表一)");
	maps3.put("data",businesslist1);
	maps4.put("title", "税源重点监测企业税收完成情况(表二)");
	maps4.put("data",businesslist2);
	}
	if(isuts.getIsUser(filter.getUserId())&&ObjectUtils.isNotBlank(ctList)){
	lists.add(maps3);
	//lists.add(maps4);
	}
	maps.put("title","税收收入完成情况");
	if(ObjectUtils.isBlank(filter.getDate())){
		maps.put("date", time);
	}else{
		 String month=filter.getDate().substring(4,6);
		 String year=filter.getDate().substring(0,4);
		 time=year+"年"+month+"月";
		maps.put("date", time);
	}
	maps.put("name","(地方+中央全口径)");
	maps.put("lists", lists);
	return coverMessage("200","数据获取成功",maps);
   }
	
	    
	
	
	
	/**
	 * 分业税收完成情况详细
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/queryTertiaryIndustry")
    public Message queryTertiaryIndustry(@RequestBody ResourcesFilter filter){
	    Map<String, Object> map = new HashMap<String, Object>();
    	List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
    	List<Map<String, Object>> incomelists = new ArrayList<Map<String,Object>>();
    	IndustryRevenue ir=industryRevenueService.getById(filter.getSysId());
    	List<IndustryRevenue> irlist=industryRevenueService.queryBySysId(filter.getSysId());
    	for(IndustryRevenue irss:irlist){
    		Map<String, Object> map2 = new HashMap<String, Object>();
    		map2.put("name", irss.getName());
    		map2.put("currentYear", irss.getYearIncome());
    		incomelists.add(map2);
    		Map<String, Object> map3 = new HashMap<String, Object>();
    		map3.put("name",irss.getName());
    		map3.put("currentYear", irss.getYearIncome());
        	map3.put("lastYear", irss.getLastyearIncome());
        	map3.put("increDecre", irss.getIncreDecre());
        	map3.put("totalRatio", irss.getTotalRatio());
        	map3.put("units", irss.getUnits());
        	list.add(map3);
    	}
    	map.put("title", ir.getName());
    	map.put("currentYear", ir.getYearIncome());
    	map.put("lastYear", ir.getLastyearIncome());
    	map.put("increDecre", ir.getIncreDecre());
    	map.put("totalRatio", ir.getTotalRatio());
    	map.put("units", ir.getUnits());
    	map.put("currentYearList",incomelists);
    	map.put("data",list);
    	return coverMessage("200","数据获取成功",map);
    }
	/**
	 * 财政收入完成情况
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/financeIncomeData")
	public Message queryFinanceIncomeDetailed(){
	    Map<String, Object> map = new HashMap<String, Object>();
    	List<Map<String, Object>> lists = new ArrayList<Map<String,Object>>();
    	//List<FinanceIncome> fiList= new ArrayList<FinanceIncome>();
    	//数据库取数据
    	 List<FinanceIncome>   fiList= financeIncomeService.queryList();
    	/* for(FinanceIncome fi:fiList1){
    		 if("11".equals(fi.getMonth())){
    			 fiList.add(fi);
    		 }
    	 }*/
    	 
    	//地方公共财政收入情况
    	 Map<String, Object> map2 = new HashMap<String, Object>();
    	 List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
    	 
    	 map2.put("data",list);
    	 
    	//地方税性收入占比情况
    	 Map<String, Object> map3 = new HashMap<String, Object>();
    	 List<Map<String, Object>> list2 = new ArrayList<Map<String,Object>>();
    	// map3.put("data", list2);
    	
    	 //一般公共预算总收入情况
    	 Map<String, Object> map4 = new HashMap<String, Object>();
    	 List<Map<String, Object>> list3 = new ArrayList<Map<String,Object>>();
    	 map4.put("data", list3);
    	 
    	 for(FinanceIncome fi:fiList){
    		 Map<String, Object> map5 = new HashMap<String, Object>();
    		    map5.put("monthIncome", fi.getCurrentMonthIncome());       //本月收入
    		    map5.put("yearIncome", fi.getCurrentYearIncome());         //本年累计收入
    		    map5.put("completeBudget", fi.getCompleteBudget());        //完成年初预算
    		    map5.put("lastYearIncome", fi.getCurrentLastYearIncome());  //上年累计收入
    		    map5.put("increDecre", fi.getIncreDecre());                 //同比增减
    		    map5.put("date",fi.getMonth());
    		 if("税收收入小计".equals(fi.getName())){
    			 map2.put("type", "1");
    			 map2.put("title", "非税收收入各月小计");
    			 map2.put("units",fi.getUnits()); 
    			 map2.put("beginBudget",fi.getBegingBudget()); 
    			 map2.put("name", fi.getName());
    			 list.add(map5);
    		 }/*else if("地方税性收入占比".equals(fi.getName())){
    			 map3.put("type", "2");
    			 map3.put("title", "地方税性收入占比情况");
    			 map3.put("units",fi.getUnits()); 
    			 map3.put("beginBudget",fi.getBegingBudget()); 
    			 map3.put("name", fi.getName());
    			 list2.add(map5);
    		 }*/else if("非税收入".equals(fi.getName())){
    			 map4.put("type", "1");
    			 map4.put("title", "非税收收入各月小计");
    			 map4.put("units",fi.getUnits()); 
    			 map4.put("beginBudget",fi.getBegingBudget()); 
    			 map4.put("name", fi.getName()+"小计");
    			 list3.add(map5);
    		 }
    		 
    	 }
    	 
    	//最后拼接
    	lists.add(map2);
    	//lists.add(map3);
    	lists.add(map4);
    	map.put("title","财政收入完成情况");
    	map.put("lists", lists);
      return coverMessage("200","数据获取成功",map);
	}
	/**
	 * 税源重点监测企业税收完成情况表
	 * @param filter
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/corporateTaxationDetail")
    public Message  queryCorporateTaxationDetail(@RequestBody IndicatorSchedulingFilter filter){
		 Map<String, Object> maps = new HashMap<String, Object>();
    	List<Map<String, Object>> businesslist = new ArrayList<Map<String,Object>>();
    	List<CorporateTaxation> ctList=new ArrayList<CorporateTaxation>();
    	List<CorporateTaxation> ctList1=corporateTaxationService.query();
    	for(CorporateTaxation ct: ctList1){
    		if("11".equals(ct.getMonth())){
    			ctList.add(ct);
    		}
    	}
    	int s=0;
    	int length=20;
    	if("2".equals(filter.getPage())){
    		s=20;
    		length=ctList.size();
    	}
    	for(int i=s;i<length; i++){
    		CorporateTaxation ct=ctList.get(i);
    		 Map<String, Object> map = new HashMap<String, Object>();
    		map.put("id", ct.getSysId());
			map.put("name", ct.getName());
			map.put("accumulativeStorage",ct.getAccumulativeStorage());
			map.put("increDecreValue",ct.getIncreDecreValue());
			map.put("increDecreaRatio",ct.getIncreDecreaRatio());
			map.put("industry", ct.getIndustry());
			businesslist.add(map);
    	}
    	maps.put("lists",businesslist);
    	maps.put("title","详细数据一览");
    	maps.put("table","税源重点监测企业税收完成情况表");
    	maps.put("units",ctList.get(0).getUnits());
    	return coverMessage("200","数据获取成功",maps);
    	
    }
	@ResponseBody
	@RequestMapping("/financeIncomeDataDetail")
	public Message  financeIncomeDataDetail(@RequestBody IndicatorBreakdownFilter filter){
		  Map<String, Object> maps = new HashMap<String, Object>();
		 List<FinanceIncome> irlist=new ArrayList<FinanceIncome>();
		 String time="";
		 List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		 if(ObjectUtils.isBlank(filter.getDate())){
			 for(int j=0;j<RevenueController.years.length;j++){
		  	   for(int i=0; i<RevenueController.months.length;i++){
		  		 FinanceIncome fi=new FinanceIncome();
		  		 if("0".equals(filter.getState())){
		  		 fi= financeIncomeService.queryByName("税收收入小计",years[j],months[i]);
		  		 }else if("1".equals(filter.getState())){
		  			 fi= financeIncomeService.queryByName("非税收入",years[j],months[i]); 
		  		 }
		  		 if(ObjectUtils.isNotBlank(fi)){
		  			 irlist=financeIncomeService.queryListByParentId(fi.getSysId());
		  		 }
		  		 
		  		 if(ObjectUtils.isNotBlank(irlist)){
		   			time=years[j]+"年"+months[i]+"月";
		   			break;
		         }
		  	   }
	      }
		 }else{
			 String month=filter.getDate().substring(4,6);
			 String year=filter.getDate().substring(0,4);
			 time=year+"年"+month+"月";
			 FinanceIncome fi=new FinanceIncome();
			 if("0".equals(filter.getState())){
		  		 fi= financeIncomeService.queryByName("税收收入小计",year,month);
		  	  }else if("1".equals(filter.getState())){
		  			 fi= financeIncomeService.queryByName("非税收入",year,month); 
		     }
			 if(ObjectUtils.isNotBlank(fi)){
	  			 irlist=financeIncomeService.queryListByParentId(fi.getSysId());
	  		 }
		 }
		if(ObjectUtils.isNotBlank(irlist)){
			for(FinanceIncome fi:irlist){
				Map<String, Object> map5 = new HashMap<String, Object>();
				map5.put("name", fi.getName());
    		    map5.put("monthIncome", fi.getCurrentMonthIncome());       //本月收入
    		    map5.put("yearIncome", fi.getCurrentYearIncome());         //本年累计收入
    		    map5.put("completeBudget", fi.getCompleteBudget());        //完成年初预算
    		    map5.put("lastYearIncome", fi.getCurrentLastYearIncome());  //上年累计收入
    		    map5.put("increDecre", fi.getIncreDecre());                 //同比增减
    		    map5.put("beginBudget", fi.getBegingBudget());
    		    map5.put("increCumulative", fi.getIncreCumulative());
    		    map5.put("date",fi.getMonth());
    		    list.add(map5);
			}
		}
		 if("0".equals(filter.getState())){
			 maps.put("title", "税收收入小计");
		 }else if("1".equals(filter.getState())){
			 maps.put("title", "非税收入");
		 }
		maps.put("date",time);
		maps.put("data",list);
		
		
	   return coverMessage("200","数据获取成功",maps);
	}
	@ResponseBody
	@RequestMapping("/financeIncomeDataDetail2")
	public Message  financeIncomeDataDetail2(@RequestBody IndicatorBreakdownFilter filter) {
		Map<String, Object> maps = new HashMap<String, Object>();
		List<Map<String, Object>> lists = new ArrayList<Map<String,Object>>();
		 List<FinanceIncome> irlist=new ArrayList<FinanceIncome>();
		 String time="";
		 FinanceIncome fis=new FinanceIncome();
		 List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		 if(ObjectUtils.isBlank(filter.getDate())){
			 for(int j=0;j<RevenueController.years.length;j++){
		  	   for(int i=0; i<RevenueController.months.length;i++){
		  		 fis=new FinanceIncome();
		  		 if("0".equals(filter.getState())){
		  		 fis= financeIncomeService.queryByName("税收收入小计",years[j],months[i]);
		  		 }else if("1".equals(filter.getState())){
		  			 fis= financeIncomeService.queryByName("非税收入",years[j],months[i]); 
		  		 }
		  		 if(ObjectUtils.isNotBlank(fis)){
		  			 irlist=financeIncomeService.queryListByParentId(fis.getSysId());
		  		 }
		  		 
		  		 if(ObjectUtils.isNotBlank(irlist)){
		   			time=years[j]+"年"+months[i]+"月";
		   			break;
		         }
		  	   }

				 if(ObjectUtils.isNotBlank(irlist)){
					// time=years[j]+"年"+months[i]+"月";
					 break;
				 }
	      }
		 }else{
			 String month=filter.getDate().substring(4,6);
			 String year=filter.getDate().substring(0,4);
			 time=year+"年"+month+"月";
			 
			 if("0".equals(filter.getState())){
		  		 fis= financeIncomeService.queryByName("税收收入小计",year,month);
		  	  }else if("1".equals(filter.getState())){
		  			 fis= financeIncomeService.queryByName("非税收入",year,month); 
		     }
			 if(ObjectUtils.isNotBlank(fis)){
	  			 irlist=financeIncomeService.queryListByParentId(fis.getSysId());
	  		 }
		 }
		if(ObjectUtils.isNotBlank(irlist)){
			for(FinanceIncome fi:irlist){
				Map<String, Object> map5 = new HashMap<String, Object>();
				map5.put("name", fi.getName());
    		    map5.put("monthIncome", fi.getCurrentMonthIncome());       //本月收入
    		    map5.put("yearIncome", fi.getCurrentYearIncome());         //本年累计收入
    		    map5.put("completeBudget", fi.getCompleteBudget());        //完成年初预算
    		    map5.put("lastYearIncome", fi.getCurrentLastYearIncome());  //上年累计收入
    		    map5.put("increDecre", fi.getIncreDecre());                 //同比增减
    		    map5.put("beginBudget", fi.getBegingBudget());
    		    map5.put("increCumulative", fi.getIncreCumulative());
    		    map5.put("date",fi.getMonth());
    		    map5.put("units", fi.getUnits());
    		    list.add(map5);
			}
		}
		
		Map<String, Object> map1 = new HashMap<String, Object>();
		Map<String, Object> map2 = new HashMap<String, Object>();
		Map<String, Object> map3 = new HashMap<String, Object>();
	  if(ObjectUtils.isNotBlank(fis)){
		map1.put("data", list);
		map1.put("type", "1");
		map1.put("title", "该指标占比情况");
		map1.put("name", fis.getName());
	    map1.put("monthIncome", fis.getCurrentMonthIncome());       //本月收入
	    map1.put("yearIncome", fis.getCurrentYearIncome());         //本年累计收入
	    map1.put("completeBudget", fis.getCompleteBudget());        //完成年初预算
	    map1.put("lastYearIncome", fis.getCurrentLastYearIncome());  //上年累计收入
	    map1.put("increDecre", fis.getIncreDecre());                 //同比增减
	    map1.put("increCumulative", fis.getIncreCumulative());
	    map1.put("date",fis.getMonth());
	    map1.put("units", fis.getUnits());
	    map2.put("data", list);
		map2.put("type", "2");
	    map2.put("beginBudget", fis.getBegingBudget());
	    map2.put("title", "该指标当月值、累计值情况");
	    map2.put("name", fis.getName());
	    map2.put("monthIncome", fis.getCurrentMonthIncome());       //本月收入
	    map2.put("yearIncome", fis.getCurrentYearIncome());         //本年累计收入
	    map2.put("completeBudget", fis.getCompleteBudget());        //完成年初预算
	    map2.put("lastYearIncome", fis.getCurrentLastYearIncome());  //上年累计收入
	    map2.put("increDecre", fis.getIncreDecre());                 //同比增减
	    map2.put("beginBudget", fis.getBegingBudget());
	    map2.put("increCumulative", fis.getIncreCumulative());
	    map2.put("date",fis.getMonth());
	    map2.put("units", fis.getUnits());
	    
	    map3.put("data", list);
		map3.put("type", "3");
	    map3.put("beginBudget", fis.getBegingBudget());
	    map3.put("title", "该指标去年同期比较情况");
		map3.put("name", fis.getName());
		map3.put("monthIncome", fis.getCurrentMonthIncome());       //本月收入
	    map3.put("yearIncome", fis.getCurrentYearIncome());         //本年累计收入
		map3.put("completeBudget", fis.getCompleteBudget());        //完成年初预算
		map3.put("lastYearIncome", fis.getCurrentLastYearIncome());  //上年累计收入
		map3.put("increDecre", fis.getIncreDecre());                 //同比增减
		map3.put("beginBudget", fis.getBegingBudget());
		map3.put("increCumulative", fis.getIncreCumulative());
		map3.put("date",fis.getMonth());
	    map3.put("units", fis.getUnits());
	    lists.add(map1);
		lists.add(map2);
		lists.add(map3);
		 }
		
		 if("0".equals(filter.getState())){
			 maps.put("title", "税收收入小计");
		 }else if("1".equals(filter.getState())){
			 maps.put("title", "非税收入");
		 }
		maps.put("date",time);
		maps.put("lists", lists);
	   return coverMessage("200","数据获取成功",maps);
	}
	
}
